package com.wsoft.cms.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.cms.entity.CmsAnnouncementCommentEntity;
import com.wsoft.cms.mapper.CmsAnnouncementCommentMapper;
import com.wsoft.cms.query.comment.CommentListInfoQuery;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/11/27 10:42
 */
@Slf4j
@Service
public class CmsAnnouncementCommentManager extends ServiceImpl<CmsAnnouncementCommentMapper, CmsAnnouncementCommentEntity> {

    @Resource
    private RedisService redisService;


//    /**
//     * 跟新评论
//     * @param entity
//     * @return
//     */
//    @Override
//    public boolean updateById(CmsAnnouncementCommentEntity entity){
//        boolean b = super.updateById(entity);
//        if(b){
//            redisService.del(RedisKeyConstant.Cms.CMS_ANNO_COMMENT_INFO + entity.getId());
//        }
//        return b;
//    }

    /**
     * 批量跟新
     * @param list
     * @return
     */
    public boolean updateBatchById(List<CmsAnnouncementCommentEntity> list){
        boolean b = super.updateBatchById(list);
        if(b){
            for (CmsAnnouncementCommentEntity commentEntity : list) {
                redisService.del(RedisKeyConstant.Cms.CMS_ANNO_COMMENT_INFO + commentEntity.getId());
            }
        }
        return b;
    }


    /**
     * 根据id删除
     * @param id
     * @return
     */
    public boolean delById(Long id){
        CmsAnnouncementCommentEntity comment = getById(id);
        boolean b = super.removeById(id);
        if(b){
            redisService.del(RedisKeyConstant.Cms.CMS_ANNO_COMMENT_INFO + id);
            redisService.del(RedisKeyConstant.Cms.CMS_ANNO_SUMMARY_INFO+comment.getAnnoId());
        }
        return b;
    }

    /**
     * 根据id查询
     * @param commentId
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Cms.CMS_ANNO_COMMENT_INFO,
            value = RedisKeyConstant.REDIS_SELF)
    public CmsAnnouncementCommentEntity getById(Long commentId){
        return super.getById(commentId);
    }

    /**
     * 查询集合
     * @param idList
     * @return
     */
    public List<CmsAnnouncementCommentEntity> getById(List<String> idList){
        List<CmsAnnouncementCommentEntity>  list  = new ArrayList<>();
        for (String id : idList) {
            list.add(this.getById(Long.valueOf(id)));
        }
        return list;
    }


    /**
     * 获取子评论分页
     * @param commentId
     * @return
     */
    public List<CmsAnnouncementCommentEntity> getChildPage(Long commentId,Integer limitCount) {
        QueryWrapper<CmsAnnouncementCommentEntity> query = new QueryWrapper<>();
        query.lambda().like(CmsAnnouncementCommentEntity::getPath,commentId)
                .orderByDesc(CmsAnnouncementCommentEntity::getCommentTime);
        if(null != limitCount){
            query.last("limit " + limitCount);
        }
        return list(query);
    }

    /**
     * 获取主评论分页
     * @param commentId
     * @return
     */
    public List<CmsAnnouncementCommentEntity> getMainPage(Long annoId) {
        QueryWrapper<CmsAnnouncementCommentEntity> query = new QueryWrapper<>();
        query.lambda().like(CmsAnnouncementCommentEntity::getAnnoId,annoId)
                .isNull(CmsAnnouncementCommentEntity::getCommentId)
                .orderByDesc(CmsAnnouncementCommentEntity::getCommentTime);
        return list(query);
    }

    /**
     * 后台 - 获取评论分页
     * @param param
     * @return
     */
    public List<CmsAnnouncementCommentEntity> listCommon(CommentListInfoQuery param) {
        QueryWrapper<CmsAnnouncementCommentEntity> query = new QueryWrapper<>();
        query.lambda().like(CmsAnnouncementCommentEntity::getAnnoId,param.getAnnoId())
                .orderByDesc(CmsAnnouncementCommentEntity::getCommentTime);
        return list(query);
    }


    // ################################## 私有方法  ##################################





}
